package top.dark.enbedmq;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;
import java.util.Scanner;

public class JmsProduce {
    static final Scanner input = new Scanner(System.in);
    private static final String ACTIVEMQ_URL = "tcp://127.0.0.1:61616";
    private static final String ACTIVEMQ_QUEUE01 = "queue01";
    public static void main(String[] args) throws JMSException {
        // 1、获得一个Activemq连接工厂,按照给定的URL，采用默认的用户名密码
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(ACTIVEMQ_URL);
        // 2、通过连接工厂,获得connection并启动访问
        Connection connection = factory.createConnection();
        connection.start();
        // 3、创建会话session 两个参数 boolean transacted=事务, int acknowledgeMode=确认模式（签收）
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        // 4、创建目的地(具体是队列queue还是主题topic)
        Queue queue = session.createQueue(ACTIVEMQ_QUEUE01);
        // 5、创建消息的生产者
        MessageProducer producer = session.createProducer(queue);
        producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        // 6、通过使用消息生产者，产生三条消息，发送到MQ队列里面
        for (; ; ) {
            TextMessage textMessage;
            System.out.println("\n请输入");
            String msgText = input.next();
            if ("exit".equals(msgText)) {
                break;
            }
            if (msgText.startsWith("auto:")) {
                int amount;
                try {
                    amount = Integer.parseInt(msgText.substring(5));
                } catch (NumberFormatException e) {
                    System.out.println("auto指令格式错误，正确的应该是：auto:number");
                    continue;
                }
                System.out.println("请稍等...");
                for (int i = 0; i < amount; i++) {
                    //7.创建消息
                    textMessage = session.createTextMessage(msgText + " " + i);//理解为一个字符串
                    //8.通过messageProducer发送给MQ队列
                    producer.send(textMessage);
                }
                System.out.println(msgText + "指令执行完成");
            }else {
                //7.创建消息
                textMessage = session.createTextMessage(msgText);//理解为一个字符串
                //8.通过messageProducer发送给MQ队列
                producer.send(textMessage);
            }
        }

        //9.关闭资源
        producer.close();
        session.close();
        System.out.println("****消息发布到MQ队列完成");
        System.exit(-1);
    }
}
